package ma.fizazi.missionsmanagement.presentation.controllers;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ma.fizazi.missionsmanagement.business.clients.ClientBusiness;
import ma.fizazi.missionsmanagement.business.clients.ContactBusiness;
import ma.fizazi.missionsmanagement.business.commons.CommonBusiness;
import ma.fizazi.missionsmanagement.business.demandes.DemandeBusiness;
import ma.fizazi.missionsmanagement.business.utilisateurs.UtilisateurBusiness;
import ma.fizazi.missionsmanagement.model.Activite;
import ma.fizazi.missionsmanagement.model.Client;
import ma.fizazi.missionsmanagement.model.Contact;
import ma.fizazi.missionsmanagement.model.Demande;
import ma.fizazi.missionsmanagement.model.NatureMission;
import ma.fizazi.missionsmanagement.model.TypeMission;
import ma.fizazi.missionsmanagement.model.Utilisateur;
import ma.fizazi.missionsmanagement.model.Valeur;
import ma.fizazi.missionsmanagement.presentation.components.ClientEditor;
import ma.fizazi.missionsmanagement.presentation.components.ContactEditor;
import ma.fizazi.missionsmanagement.presentation.components.NatureDemandeEditor;
import ma.fizazi.missionsmanagement.presentation.components.UtilisateurEditor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/demandes")
public class DemandeController {

	@Autowired
	CommonBusiness commonBusiness;
	@Autowired
	DemandeBusiness demandeBusiness;
	@Autowired
	ClientBusiness clientBusiness;
	@Autowired
	ContactBusiness contactBusiness;
	@Autowired
	UtilisateurBusiness utilisateurBusiness;

	@Autowired
	ClientEditor clientEditor;
	@Autowired
	ContactEditor contactEditor;
	@Autowired
	UtilisateurEditor utilisateurEditor;
	@Autowired
	NatureDemandeEditor natureDemandeEditor;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
		binder.registerCustomEditor(Client.class, this.clientEditor);
		binder.registerCustomEditor(Contact.class, this.contactEditor);
		binder.registerCustomEditor(Utilisateur.class, this.utilisateurEditor);
		binder.registerCustomEditor(NatureMission.class, this.natureDemandeEditor);
	}

	@RequestMapping(method = RequestMethod.GET)
	public String ticketsPage(ModelMap model) {
		List<Demande> ticketsList = demandeBusiness.getAllDemande();
		model.addAttribute("demandesList", ticketsList);
		return "demandes";
	}

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String newTicketPage(ModelMap model) {

		return "newDemande";
	}

	@RequestMapping(value = "/new", method = RequestMethod.POST)
	public String addNewTicket(@ModelAttribute("newDemande") Demande demande,
			ModelMap model) {

		List<Valeur> valeurs = demandeBusiness.getValeurByDemande(demande.getId());
		demande.setValeurs(valeurs);
		commonBusiness.saveEntity(demande);
		return "redirect:/demandes";
	}

	@RequestMapping(value = "/me", method = RequestMethod.GET)
	public String mesTicketsPage(ModelMap model, Principal principal) {

		List<Activite> ticketsList = null;
		String name = principal.getName();
		if (name == null || "admin".equals(name) || "resp".equals(name)
				|| "cons".equals(name))
			return "activites";
		else {
			ticketsList = utilisateurBusiness.getActiviteByUtilisateur(name);
		}

		model.addAttribute("demandesList", ticketsList);
		return "mesDemandes";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String deleteTicket(@RequestParam("id") int ticketId, ModelMap model) {
		Demande dat = demandeBusiness.getDemandeById(ticketId);
		commonBusiness.deleteEntity(dat);
		return "redirect:/demandes";
	}

	// @ModelAttribute("ticketsList")
	// public List<DAT> getAllTicketList() {
	// return ticketBusiness.getAllDats();
	// }

	@ModelAttribute("newDemande")
	public Demande getNewDemande() {
		return new Demande();
	}

	@ModelAttribute("clientsList")
	public Map<String, String> getClientList() {
		List<Client> activeClients = null;
		activeClients = clientBusiness.getAllClients();
		Map<String, String> clientsList = new HashMap<String, String>();
		if (activeClients != null) {
			for (Client client : activeClients) {
				clientsList.put(client.getId() + "", client.getNomClient());
			}
		}
		return clientsList;
	}

	@ModelAttribute("natureList")
	public Map<String, String> getNatureDemandeList() {
		List<NatureMission> familleDeProduits = demandeBusiness.getAllNatureDemande();
		Map<String, String> pfList = new HashMap<String, String>();

		if (familleDeProduits != null) {
			for (NatureMission familleDeProduit : familleDeProduits) {
				pfList.put(familleDeProduit.getId() + "",
						familleDeProduit.getNature());
			}
		}
		return pfList;
	}

	@ModelAttribute("typeList")
	public Map<String, String> getTypeDemandeList() {
		List<TypeMission> familleDeProduits = demandeBusiness.getAllTypeDemandeById();
		Map<String, String> pfList = new HashMap<String, String>();

		if (familleDeProduits != null) {
			for (TypeMission familleDeProduit : familleDeProduits) {
				pfList.put(familleDeProduit.getId() + "",
						familleDeProduit.getType());
			}
		}
		return pfList;
	}

	@ModelAttribute("consultantsList")
	public Map<String, String> getConsultantsList() {
		List<Utilisateur> consultants = null;
		consultants = utilisateurBusiness.getActiveConsultants();
		Map<String, String> consultantsList = new HashMap<String, String>();
		if (consultants != null) {
			for (Utilisateur consultant : consultants) {
				consultantsList.put(consultant.getId() + "",
						consultant.getNom() + " " + consultant.getPrenom());
			}
		}
		return consultantsList;
	}


	@ModelAttribute("utilisateurs")
	public List<Utilisateur> getUtilisateurs() {
		List<Utilisateur> utilisateurs = utilisateurBusiness
				.getAllConsultants();
		return utilisateurs;
	}
}
